package com.abe.libquick.widget.form;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.abe.libquick.R;
import com.abe.libquick.utils.NormalUtils;

@SuppressLint("NewApi")
public class FormSwitchView extends RelativeLayout {
    private TextView itemLabelName;
    private Switch itemSwitch;
    private String label, turnOn, turnOff;

    public FormSwitchView(Context context) {
        this(context, null);
    }

    public FormSwitchView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FormSwitchView(Context context, AttributeSet attributeSet, int defaultStyle) {
        super(context, attributeSet, defaultStyle);
        //抽取需要的自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.FormSwitchView);
        int labelRes = typedArray.getResourceId(R.styleable.FormSwitchView_labelRes, 0);
        String labelStr = typedArray.getString(R.styleable.FormSwitchView_label);
        int turnOnRes = typedArray.getResourceId(R.styleable.FormSwitchView_turnOnRes, 0);
        String turnOnStr = typedArray.getString(R.styleable.FormSwitchView_turnOn);
        int turnOffRes = typedArray.getResourceId(R.styleable.FormSwitchView_turnOffRes, 0);
        String turnOffStr = typedArray.getString(R.styleable.FormSwitchView_turnOff);
        label = format(labelStr, labelRes);
        turnOn = format(turnOnStr, turnOnRes);
        turnOff = format(turnOffStr, turnOffRes);
        float labelSize = typedArray.getDimension(R.styleable.FormSwitchView_labelSize, NormalUtils.dip2px(getContext(), 16));
        boolean canEdit = typedArray.getBoolean(R.styleable.FormSwitchView_canEdit, true);
        boolean showText = typedArray.getBoolean(R.styleable.FormSwitchView_showText, true);
        boolean checked = typedArray.getBoolean(R.styleable.FormSwitchView_checked, true);
        typedArray.recycle();
        View view = View.inflate(context, R.layout.item_abe_view_common_form_switch, this);
        itemLabelName = view.findViewById(R.id.item_label_name);
        itemSwitch = view.findViewById(R.id.item_switch_value);
        itemLabelName.setText(label);
        itemLabelName.setTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize);
        itemSwitch.setChecked(checked);
        itemSwitch.setShowText(showText);
        itemSwitch.setTextOff(turnOff);
        itemSwitch.setTextOn(turnOn);
        itemSwitch.setEnabled(canEdit);
    }

    public Switch getItemSwitch() {
        return itemSwitch;
    }

    public void setLabel(String label) {
        this.label = label;
        if (itemLabelName != null) {
            itemLabelName.setText(label);
        }
    }

    public void setLabelRes(int labelRes) {
        setLabel(getContext().getResources().getString(labelRes));
    }

    public void setTurnOn(String turnOn) {
        this.turnOn = turnOn;
        if (itemSwitch != null) {
            itemSwitch.setTextOn(turnOn);
        }
    }

    public void setTurnOnRes(int turnOnRes) {
        setTurnOn(getContext().getResources().getString(turnOnRes));
    }

    public void setTurnOff(String turnOff) {
        this.turnOff = turnOff;
        if (itemSwitch != null) {
            itemSwitch.setTextOff(turnOff);
        }
    }

    public void setShowText(boolean showText) {
        if (itemSwitch != null) {
            itemSwitch.setShowText(showText);
        }
    }

    public void setChecked(boolean checked) {
        if (itemSwitch != null) {
            itemSwitch.setChecked(checked);
        }
    }

    public void setTurnOffRes(int turnOffRes) {
        setTurnOff(getContext().getResources().getString(turnOffRes));
    }


    private String format(String str, int res) {
        String value = "";
        if (res != 0) {
            value = getContext().getResources().getString(res);
        }
        if (!TextUtils.isEmpty(str)) {
            value = str;
        }
        return value;
    }
}